package com.firefly.admin.system.controller;

import com.firefly.admin.common.annotations.annotation.OperationLog;
import com.firefly.admin.common.enums.OperationModuleEnum;
import com.firefly.admin.common.enums.OperationTypeEnum;
import com.firefly.admin.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.firefly.admin.system.entity.FySystemOrganization;
import com.firefly.admin.system.service.FySystemOrganizationService;
import com.firefly.admin.system.vo.GroupSelect;
import com.firefly.admin.system.vo.OrgTree;
import com.firefly.admin.system.vo.SystemOrgQueryVo;
import com.firefly.admin.system.vo.SystemOrgStatusUpdateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "系统组织机构接口API")
@RestController
@RequestMapping("/system/org")
public class FySystemOrganizationController {

    @Autowired
    private FySystemOrganizationService fySystemOrganizationService;

    /**
     * 查询系统组织机构列表接口
     *
     * @param systemOrgQueryVo 查询参数
     * @return 列表
     */
    @ApiOperation(value = "查询系统组织机构列表接口", notes = "查询系统组织机构列表接口", httpMethod = "GET")
    @PreAuthorize(value = "hasAuthority('system:org')")
    @GetMapping("/list")
    public Result<List<FySystemOrganization>> getSystemOrgList(SystemOrgQueryVo systemOrgQueryVo) {
        return fySystemOrganizationService.getSystemOrgList(systemOrgQueryVo);
    }

    /**
     * 系统组织机构信息新增接口
     *
     * @param fySystemOrganization 组织机构信息
     * @return 操作结果
     */
    @ApiOperation(value = "系统组织机构信息新增接口", notes = "系统组织机构信息新增接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_ORG, operateDesc = "系统组织机构新增")
    @PreAuthorize(value = "hasAuthority('system:org:add')")
    @PostMapping("/add")
    public Result<FySystemOrganization> add(@RequestBody FySystemOrganization fySystemOrganization) {
        return fySystemOrganizationService.add(fySystemOrganization);
    }

    /**
     * 系统组织机构信息编辑接口
     *
     * @param fySystemOrganization 组织机构信息
     * @return 操作结果
     */
    @ApiOperation(value = "系统组织机构信息编辑接口", notes = "系统组织机构信息编辑接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_ORG, operateDesc = "系统组织机构编辑")
    @PreAuthorize(value = "hasAuthority('system:org:update')")
    @PostMapping("/update")
    public Result<FySystemOrganization> update(@RequestBody FySystemOrganization fySystemOrganization) {
        return fySystemOrganizationService.update(fySystemOrganization);
    }

    /**
     * 系统组织机构状态编辑接口
     *
     * @param systemOrgStatusUpdateVo 状态信息
     * @return 操作结果
     */
    @ApiOperation(value = "系统组织机构状态编辑接口", notes = "系统组织机构状态编辑接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_ORG, operateDesc = "系统组织机构修改状态")
    @PreAuthorize(value = "hasAuthority('system:org:update:status')")
    @PostMapping("/update/status")
    public Result<FySystemOrganization> updateStatus(@RequestBody SystemOrgStatusUpdateVo systemOrgStatusUpdateVo) {
        return fySystemOrganizationService.updateStatus(systemOrgStatusUpdateVo);
    }

    /**
     * 系统组织机构信息删除接口
     *
     * @param orgId 组织机构ID
     * @return 操作结果
     */
    @ApiOperation(value = "系统组织机构信息删除接口", notes = "系统组织机构信息删除接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", required = true, value = "组织机构ID", paramType = "query", dataType = "Long")
    })
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_ORG, operateDesc = "系统组织机构删除")
    @PreAuthorize(value = "hasAuthority('system:org:delete')")
    @PostMapping("/delete")
    public Result<String> delete(Long orgId) {
        return fySystemOrganizationService.delete(orgId);
    }

    /**
     * 获取组织机构树列表接口
     *
     * @return 组织机构树列表
     */
    @ApiOperation(value = "获取菜单树列表接口", notes = "获取菜单树列表接口", httpMethod = "GET")
    @GetMapping("/tree/list")
    public Result<List<OrgTree>> getOrgTree() {
        return fySystemOrganizationService.getOrgTree();
    }

    /**
     * 通过父节点ID获取能作选负责人的账号信息
     *
     * @param parentId 父节点ID
     * @return 可选负责人账号列表
     */
    @ApiOperation(value = "通过父节点ID获取能作选负责人的账号信息", notes = "通过父节点ID获取能作选负责人的账号信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", required = true, value = "父节点ID", paramType = "query", dataType = "Long")
    })
    @GetMapping("/director/list")
    public Result<List<GroupSelect>> getDirectorList(@RequestParam("parentId") Long parentId) {
        return fySystemOrganizationService.getDirectorList(parentId);
    }
}
